﻿using AttributeRouting.Web.Http;
using ETA.Entity;
using ETA.IService;
using ETA.WebAPI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ETA.Dtos;
using ETA.Common;
using ETA.DomainEnum;
using ETA.ClientService;

namespace ETA.WebAPI.Controllers
{
    public class TraineeController : ApiController
    {
        private static IWorkTypeService _IWorkTypeService { set; get; }
        private static IWorkTypeInfoService _IWorkTypeInfoService { set; get; }
        private static IRegistrationBasicInfoService _IRegistrationBasicInfoService { set; get; }
        private static IUserService _IUserService { set; get; }
        private static IDocumentService _DocumentService { set; get; }
        private static IAskForHelpService _IAskForHelpService { set; get; }
        private static IBannerService _BannerService { set; get; }

        public TraineeController(IBannerService BannerService,IWorkTypeService IWorkTypeService, IRegistrationBasicInfoService registrationBasicInfoService
            , IUserService IUserService, IDocumentService IDocumentService, IAskForHelpService IAskForHelpService, IWorkTypeInfoService IWorkTypeInfoService)
        {
            _IWorkTypeService = IWorkTypeService;
            _IRegistrationBasicInfoService = registrationBasicInfoService;
            _IUserService = IUserService;
            _DocumentService = IDocumentService;
            _IAskForHelpService = IAskForHelpService;
            _IWorkTypeInfoService = IWorkTypeInfoService;
            _BannerService = BannerService;
        }

        /// <summary>
        /// 基本信息保存
        /// </summary>
        /// <param name="modelDto"></param>
        /// <returns></returns>
        [GET("SaveBasicInfo")]
        public ApiResDTO GetSaveBasicInfo([FromUri]RegistrationBasicInfoReqDTO modelDto)
        {
            var currentUser = GetUserInfo(modelDto.UserId);
            User model = RegistrationBasicInfoDtoConvert(modelDto, currentUser);
            var res = new ApiResDTO();
            if (ModelState.IsValid)
            {
                var modifyUserRes = _IUserService.ModifyUser(new ModifyUserRequest
                {
                    User = model
                });
                if (modifyUserRes.Result.IsSuccess)
                {
                    res.Result = ResultEnum.Success;
                }
                else
                {
                    res.Result = ResultEnum.Fail;
                    res.ResultDesc = modifyUserRes.Result.ErrMessage;
                }
            }
            return res;
        }

        /// <summary>
        /// 根据id返回用户信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        private User GetUserInfo(string userId)
        {
            var req = new QueryUserRequest
            {
                User = new User
                {
                    UserId = userId
                }
            };
            var response = _IUserService.QueryUser(req);
            if (response.Result.IsSuccess)
            {
                return response.Users.FirstOrDefault();
            }
            return null;
        }

        private User RegistrationBasicInfoDtoConvert(RegistrationBasicInfoReqDTO dto, User user)
        {
            user.UserName = dto.Name;
            user.SexEnum = (DomainEnum.SexEnum)(dto.Sex);
            user.Age = dto.Age;
            user.Birthday = DateTime.Parse(dto.Birth);
            user.EducationLevelEnum = (DomainEnum.EducationLevelEnum)dto.Education;
            user.OrginalJobs = dto.PreWorks;
            user.NewJobs = dto.InternshipWorks;
            user.IDCard = dto.IDCard;
            user.Mobile = dto.Phone;
            user.QQ = dto.QQ;
            user.WeiXin = dto.Weixin;
            user.Ahousehold = dto.HouseHold;
            return user;
        }

        /// <summary>
        /// 返回所有工种以及工种对应的项目
        /// </summary>
        /// <returns></returns>
        [GET("GetWorkType")]
        public ApiResDTO GetWorkType()
        {
            var res = new ApiResDTO();
            var response = _IWorkTypeService.QueryWorkTypeList(new QueryWorkTypeRequest());
            if (response.Result.IsSuccess)
            {
                res.Result = ResultEnum.Success;
                res.ResultDesc = response.WorkTypeList;
            }
            return res;
        }

        /// <summary>
        /// 返回所有工种以及工种对应的项目
        /// </summary>
        /// <returns></returns>
        [GET("GetWorkTypeInfo")]
        public ApiResDTO GetWorkTypeInfo(string WorkTypeId)
        {
            var res = new ApiResDTO();
            var response = _IWorkTypeInfoService.QueryWorkTypeInfoList(new QueryWorkTypeInfoRequest() { WorkTypeId = WorkTypeId });
            if (response.Result.IsSuccess)
            {
                res.Result = ResultEnum.Success;
                res.ResultDesc = response.WorkTypeInfoList;
            }
            return res;
        }

        /// <summary>
        /// 返回文档列表
        /// </summary>
        /// <returns></returns>
        [GET("GetDoc")]
        public QueryDocResDTO GetDoc()
        {
            var res = new QueryDocResDTO();
            var queryDocRes = _DocumentService.QueryDocumentList(new QueryDocumentRequest());
            if (queryDocRes.Result.IsSuccess)
            {
                res.Result = ResultEnum.Success;
                var dtoList = new List<DocDTO>();
                foreach (var doc in queryDocRes.Documents)
                {
                    dtoList.Add(ConverntDto(doc));
                }
                res.Docs = dtoList;
            }
            return res;
        }

        private DocDTO ConverntDto(Document doc)
        {
            var docDto = new DocDTO
            {
                DocNo = doc.DocNo,
                From = doc.ContentRefrence,
                Target = doc.Target,
                Ttile = doc.Title,
                Version = doc.Version,
                Url = Config.DocUrl + doc.UploadFile.FileName
            };
            return docDto;
        }

        /// <summary>
        /// 添加援助数据
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [GET("AddHelpRequest")]
        public ApiResDTO GetAddHelpRequest([FromUri]AddHelpReqDTO dto)
        {
            var res = new ApiResDTO();
            var response = _IAskForHelpService.CreateAskForHelp(
               new CreateAskForHelpRequest
               {
                   AskForHelp = new Entity.AskForHelp
                   {
                       AskForHelpContent = dto.Content,
                       SubmitUser = UserBll.GetCompltetUserInfo(dto.Account),
                       AskForHelpId = Guid.NewGuid().ToString(),
                       AskForHelpKindEnum = (DomainEnum.AskForHelpKindEnum)dto.Category,
                       AskForHelpTypeEnum = (DomainEnum.AskForHelpTypeEnum)dto.Type,
                       CreateDate = DateTime.Now
                   }
               }
            );
            if (response.Result.IsSuccess)
            {
                res.Result = ResultEnum.Success;
            }
            else
            {
                res.ResultDesc = response.Result.ErrMessage;
            }
            return res;
        }

        /// <summary>
        /// 根据用户id返回该用户相关的援助请求信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [GET("QueryHelpRequest")]
        public QueryHelpResDTO GetQueryHelpRequest(string userId = null, string RecordId = null)
        {
            var res = new QueryHelpResDTO();
            var count = 0;
            var response = _IAskForHelpService.QueryAskForHelpList(
              new QueryAskForHelpRequest
              {
                  UserId = userId,
                  Sosid = RecordId
              }, out count
             );
            if (response.Result.IsSuccess)
            {
                res.Result = ResultEnum.Success;
                var list = new List<AddHelpReqDTO>();
                foreach (var a in response.AskForHelpList)
                {
                    list.Add(ConverntHelpDto(a));
                }
                res.AcidRecords = list;
            }
            else
            {
                res.ResultDesc = response.Result.ErrMessage;
            }
            return res;
        }


        /// <summary>
        /// 援助数据转换
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private AddHelpReqDTO ConverntHelpDto(AskForHelp model)
        {
            var dto = new AddHelpReqDTO
            {
                Account = model.UserId,
                Category = (int)model.AskForHelpKindEnum,
                Type = (int)model.AskForHelpTypeEnum,
                Content = model.AskForHelpContent,
                Comment = (int)model.EvaluateEnum,
                Date = model.CreateDate.ToShortDateString(),
                RecordId = model.AskForHelpId,
                State = model.StateEnum.ToString()
            };
            return dto;
        }

        /// <summary>
        /// 评价
        /// </summary>
        /// <param name="id"></param>
        /// <param name="comment"></param>
        /// <returns></returns>
        [GET("Comment")]
        public ApiResDTO GetComment(string id, int comment)
        {
            var res = new ApiResDTO();
            var response = _IAskForHelpService.CommentAskForHelp(id, (EvaluateEnum)comment);
            if (response.Result.IsSuccess)
            {
                res.Result = ResultEnum.Success;
            }
            else
            {
                res.Result = ResultEnum.Fail;
                res.ResultDesc = response.Result.ErrMessage;
            }
            return res;
        }

        [GET("GetBanners")]
        public ApiResDTO GetBanner(int bannerKind)
        {
            var res = new ApiResDTO();
            if (bannerKind < 0 || bannerKind > 2) {
                res.ResultDesc = "参数错误";
                return res;
            }

            List<string> bannerUrls = new List<string>();
            string[] bannerKindArr = new string[] {"","App引导图", "App广告图" };
            var queryRes = _BannerService.QueryFeatureBannerList(new QueryBannerRequest
            {
                BannerKind = bannerKindArr[bannerKind]
            });
            if (queryRes.Result.IsSuccess)
            {
                var banners = queryRes.BannerList;
                foreach (var b in banners)
                {
                    bannerUrls.Add(b.BannerUrl);
                }
                res.ResultDesc = new { BannerUrls = bannerUrls };
                res.Result = ResultEnum.Success;
            }
            return res;
        }

    }
}
